# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        mdepth = leftdepth = rightdepth = 0
        if root != None:
            mdepth = mdepth+1
            if root.left != None:
                leftdepth = self.maxDepth(root.left)
            if root.right!= None:
                rightdepth = self.maxDepth(root.right)
            if leftdepth>rightdepth:
                mdepth += leftdepth
            else:
                mdepth += rightdepth
        return mdepth

